package com.ruoyi.common.wrapper;

import com.ruoyi.common.constant.HttpStatus;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;

/**
 * 通用请求返回封装Bean
 *
 * @Author: kfc
 */

@NoArgsConstructor
@Setter
@Getter
@ToString
public class CommonResult<T> extends BaseResult<T> implements Serializable {


    /**
     * 接口请求成功
     * @return
     */
    public static CommonResult success(){
        return baseCreate(HttpStatus.SUCCESS,"操作成功");
    }

    /**
     * 接口请求成功
     * @param msg  返回的自定义消息
     * @return
     */
    public static CommonResult success(String msg){
        return baseCreate(HttpStatus.SUCCESS,msg);
    }

    /**
     * 接口请求成功
     * @param data  返回的数据体
     * @return
     */
    public static <T> CommonResult<T> success(T data){
        return success().setResult(data);
    }

    /**
     * 接口请求成功
     * @param msg   自定义返回消息
     * @param data  返回的数据体
     * @return
     */
    public static <T> CommonResult<T> success(String msg,T data){
        return success(msg).setResult(data);
    }

    /**
     * 用户未认证
     *
     * @return
     */

    public static CommonResult unauthorized() {
        return baseCreate(HttpStatus.UNAUTHORIZED,"");
    }

    /**
     * 用户未认证
     * @param  msg   自定义返回消息
     * @return
     */

    public static CommonResult unauthorized(String msg) {
        return baseCreate(HttpStatus.UNAUTHORIZED,msg);
    }


    /**
     * 操作失败
     * @return
     */
    public static CommonResult error(){
        return baseCreate(HttpStatus.ERROR,"操作失败");
    }

    /**
     * 操作失败
     * @param msg 自定义返回消息
     * @return
     */
    public static CommonResult error(String msg){
        return baseCreate(HttpStatus.ERROR,msg);
    }


    private static <T> CommonResult<T> baseCreate(int code, String msg) {
        CommonResult result = new CommonResult();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public CommonResult<T> setResult(T data) {
        this.setData(data);
        return this;
    }

}
